//
//  XGNewsViewModel.swift
//  TodayNews
//
//  Created by monkey on 2020/3/25.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit

enum XGNewsCellType
{
    /// 默认
    case normal
    /// 单图
    case singlePicture
    /// 三张图
    case threePictures
    /// 视频
    case video
}

class XGNewsViewModel
{
    /// 标题
    var title: String? {
        return newsModel.title
    }
    /// 新闻
    var newsUrl: String? {
        return newsModel.display_url
    }
    /// 游标
    var cursor: UInt64 {
        return newsModel.cursor
    }
    /// 图片
    var imageList: [XGImageInfo]? {
        return newsModel.image_list
    }
    var middleImage: String? {
        return newsModel.middle_image?.url
    }
    var largeImage: String? {
        return newsModel.large_image_list?.first?.url
    }
    /// 是否有视频
    var hasVideo: Bool {
        return newsModel.has_video
    }
    /// 视频id
    var videoId: String? {
        return newsModel.video_id
    }
    /// 用户信息
    var userInfo: XGUserInfo? {
        return newsModel.user_info
    }
    /// 时间 用来做刷新
    var behotTime: TimeInterval {
        return newsModel.behot_time
    }
    
    /// 视频时长
    private(set) var videoDuration: String
    /// 播放次数
    private(set) var videoWatchCount: String
    /// cell类型
    private(set) var cellType: XGNewsCellType
    /// 描述信息
    private(set) var info: String
    /// 行高
    private(set) var rowHeight: CGFloat = 0
    
    init(newsModel: XGNewsModel)
    {
        if newsModel.has_video {
            cellType = .video
        } else if newsModel.has_image {
            cellType = (newsModel.image_list?.count ?? 0) >= 3 ? .threePictures : .singlePicture
        } else {
            // 没图
            cellType = .normal
        }

        info = (newsModel.source ?? "") + "  "
        info += (newsModel.read_count.countString) + "阅读" + "  "
        info += Date.convertToCreateString(sinceNow: TimeInterval(newsModel.publish_time))
        videoDuration = String(format: "%02d", newsModel.video_duration / 60) + ":" + String(format: "%02d", newsModel.video_duration % 60)
        videoWatchCount = newsModel.video_watch_count.countString + "次播放"
        
        switch cellType {
         case .normal: // 普通新闻
            rowHeight += 5 + XGCalculator.calculateString(text: newsModel.title!, width: TodayNews_Screen_Width - 20, fontSize: 17, maxHeight: 44)
            rowHeight = 10 + 20 + ceil(rowHeight)
        case .singlePicture: // 单图新闻
            let textHeight = XGCalculator.calculateString(text: newsModel.title!, width: TodayNews_Screen_Width - 30 - 120, fontSize: 17, maxHeight: 80)
            if textHeight < 60.0 {
                rowHeight += 5 + 80 + 5
            } else {
                rowHeight += 5 + textHeight + 10 + 20
            }
            rowHeight = ceil(rowHeight)
        case .threePictures: // 多图新闻
            rowHeight += 5 + XGCalculator.calculateString(text: newsModel.title!, width: TodayNews_Screen_Width - 20, fontSize: 17, maxHeight: 44)
            rowHeight += 10 + 80
            rowHeight = 10 + 20 + ceil(rowHeight)
        case .video:
            rowHeight = ceil(5 + 36 + 10 + TodayNews_Screen_Width / 16.0 * 9.0)
         }
        
        self.newsModel = newsModel
    }
    
    private let newsModel: XGNewsModel
}
